package org.judking.CarKeeper.command;

import java.io.Serializable;

import org.judking.CarKeeper.activity.MainActivity;

import android.util.Log;

/**
 * Service $01: powertrain diagnostic data
 * @author judking
 *
 */
public class PddObdCommand extends BaseObdCommand       {
        private Double ripeData;                                                             
        private String unit4Measure;                                   
        private Double zoomRatio = 1D;                                
        private Double translationVal = 0D;                    

        //-----------------------------------------------constructor---------------------------------------------------------//
        public PddObdCommand() {
                super("01");
        }
        
        public PddObdCommand(String PID)                {
                this();
                setPID(PID);
        }
        
        public PddObdCommand(String PID, Double zoomRatio, Double translationVal) {
                this(PID);
                this.zoomRatio = zoomRatio;
                this.translationVal = translationVal;
        }

        //-----------------------------------------------method---------------------------------------------------------//
        @Override
        public void cookData() {
                String data = getRawData();
                
                if(data.indexOf("nodata") != -1)                {
                        Log.i(TAG, "PDDObdCommand: No data got from OBD, cmd: "+genCmd());
                        ripeData = 0D;
                        return;
                }
                
                data = removeRtnHeader(data);
                long val = Long.parseLong(data, 16);
                ripeData = transformation(val, zoomRatio, translationVal);
        }

        @Override
        public boolean isCooked() {
                return ripeData != null;
        }

        @Override
        public Object getRipeData() {
                return ripeData;
        }
        
        @Override
        protected boolean isRtnDataFail() {
                for(String s : getRawData().split("\r"))                {
                        s = slimLine(s);
                        if(isRtnDataLineValid(s))               {
                                setRawData(s);
                                return false;
                        }
                }
                return true;
        }
        
        @Override
        protected void remedyRtnData() {
                setRawData("00 00 00");
        }
        
        //ÉìËõ¡¢Æ½ÒÆ±ä»»
        protected Double transformation(long val, Double mZoomRatio, Double mTranslationVal) {
                return ((double)val*mZoomRatio+mTranslationVal);
        }

        //-----------------------------------------------getter & setter---------------------------------------------------------//
        public String getUnit4Measure() {
                return unit4Measure;
        }

        public void setUnit4Measure(String unit4Measure) {
                this.unit4Measure = unit4Measure;
        }

        /**
         * 
         */
        private static final long serialVersionUID = 130227L;
}